//
//  LoginViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "LoginViewController.h"
#import "Masonry.h"
#import "SVProgressHUD.h"

@interface LoginViewController ()

//文字logo
@property (nonatomic, strong) UIImageView *logoImg2;
//用户名textField
@property (nonatomic, strong) UITextField *nameField;
//密码textField
@property (nonatomic, strong) UITextField *passwordField;

@property (nonatomic, assign) BOOL isLogin;

@end

/**
 *  BBZ zhou yang
 */
@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置登录界面
    [self setupUI];
}

#pragma mark -- 设置初始化界面
- (void)setupUI {
    
    //设置导航栏
    [self.navigationItem setTitle:@"登录"];
    
    //布局图片
    [self layoutImageView];
    
    //布局textField和登录按钮
    [self layoutTextFieldAndButton];
    
    
}

#pragma mark -- 布局图片
- (void)layoutImageView {
    
    //创建图片的logo
    UIImageView *logoImg1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product_img_logo_nor.png"]];
    
    [self.view addSubview:logoImg1];
    
    //创建文字的logo
    UIImageView *logoImg2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    
    [self.view addSubview:logoImg2];
    
    self.logoImg2 = logoImg2;
    
    //添加约束
    [logoImg1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.size.equalTo(CGSizeMake(67, 88));
    }];
    
    [logoImg2 mas_makeConstraints:^(MASConstraintMaker *make) {        
        make.top.equalTo(logoImg1.bottom).offset(10);
        make.centerX.equalTo(logoImg1);
        make.size.equalTo(CGSizeMake(83, 49));
    }];
}

#pragma mark -- 布局textField
- (void)layoutTextFieldAndButton {
    
    //创建textField
    UITextField *nameField = [self createTextFieldWithPlaceholder:@"请输入账户"];
    self.nameField = nameField;
    
    UITextField *passwordField = [self createTextFieldWithPlaceholder:@"请输入密码"];
    self.passwordField = passwordField;
    
    //密码显示为点
    passwordField.secureTextEntry = YES;
    
    //添加约束
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImg2.mas_bottom).offset(20);
        make.leading.equalTo(self.view).offset(25);
        make.trailing.equalTo(self.view).offset(-25);
    }];
    
    [passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameField.mas_bottom).offset(20);
        make.leading.trailing.equalTo(nameField);
    }];
    
    //创建button
    UIButton *loginButton = [[UIButton alloc] init];
    
    [self.view addSubview:loginButton];
    
    //button的背景颜色
    [loginButton setBackgroundColor:kThemeColor];
    
    loginButton.layer.cornerRadius = 5;
    //设置文字
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [loginButton.titleLabel setFont:kFONT16];
    
    //约束
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(nameField);
        make.top.equalTo(passwordField.bottom).offset(20);
    }];
    
    [loginButton addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //从用户偏好取出用户名和密码
    self.nameField.text = [[NSUserDefaults standardUserDefaults] objectForKey:kUserName];
    
    if(self.isPasswordEmpty == NO) {
        
        self.passwordField.text = [[NSUserDefaults standardUserDefaults] objectForKey:kPassword];
    }
    else {
        
        self.passwordField.text = nil;
    }
    
}

//封装 创建textField的方法
- (UITextField *)createTextFieldWithPlaceholder:(NSString *)placeholder {
    
    UITextField *textField = [[UITextField alloc] init];
    
    [self.view addSubview:textField];
    
    //圆角边框
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = placeholder;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    
    //清除button
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    return textField;
}

#pragma mark -- 登录button点击事件
- (void)loginBtnClick {
    
    //判断用户名和密码
    //从用户偏好中取
    NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:kPassword];
    
    if(([self.nameField.text isEqualToString:@"1"] && [self.passwordField.text isEqualToString:(password==nil?@"1":password)]) || ([self.nameField.text isEqualToString:@""] && [self.passwordField.text isEqualToString:@""])) {

        self.isLogin = YES;
        
        //保存密码到本地
        [[NSUserDefaults standardUserDefaults] setObject:self.nameField.text forKey:kUserName];
        
        [[NSUserDefaults standardUserDefaults] setObject:self.passwordField.text forKey:kPassword];
        
        //发送通知
        NSDictionary *dict = @{@"isLogin":@"1"};
        NSNotification *n = [[NSNotification alloc]initWithName:@"isLoginNotificationKey" object:nil userInfo:dict];
        [[NSNotificationCenter defaultCenter] postNotification:n];
        
        //跳回到首页
        [self.navigationController popToRootViewControllerAnimated:NO];
        
        //延时显示弹窗
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            [SVProgressHUD showSuccessWithStatus:@"登录成功" ];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [SVProgressHUD dismiss];
            });
        });
        

    }
    else {
        
        //错误提示弹框
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"账号或密码错误" ];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
    }
}


#pragma mark -- 点击屏幕,回退键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

@end














